package com.example.xianji.adapter;

import java.util.ArrayList;

import com.example.xianji.activity.Main_Bianji_QiYe_InformationActivity;
import com.example.xianji.R;
import com.example.xianji.bean.YiJi_Class;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class HangYe_Adapter extends BaseAdapter {
private Context mContext;
private ArrayList<YiJi_Class> list;
private  ArrayList<Integer> indext;
/**
 * checkboxֵ
 */
private  SparseBooleanArray array;




	public HangYe_Adapter(Context mContext, ArrayList<YiJi_Class> list,
		ArrayList<Integer> indext, SparseBooleanArray array) {
	super();
	this.mContext = mContext;
	this.list = list;
	this.indext = indext;
	this.array = array;
}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder mHolder;
		if(convertView==null){
			
			mHolder=new Holder();
			
			convertView=View.inflate(mContext, R.layout.hangye_listview_item, null);
			mHolder.textview_hangye=(TextView) convertView.findViewById(R.id.textview_hangye);
			mHolder.hangye_checkbox=(CheckBox) convertView.findViewById(R.id.hangye_checkbox);
			
			
			convertView.setTag(mHolder);
			
			
		}
		mHolder=(Holder) convertView.getTag();
		mHolder.textview_hangye.setText(list.get(position).getName());
		
		final CheckBox box=mHolder.hangye_checkbox;
		final int a=position;
		//indext.clear();
		
		mHolder.hangye_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					if(indext.contains(list.get(a).getId())){
						
					}else{
						
					indext.add(list.get(a).getId());
					YiJi_Class yiji=new YiJi_Class();
					yiji.setId(list.get(a).getId());
					yiji.setName(list.get(a).getName());
					Main_Bianji_QiYe_InformationActivity.yiji_list.add(yiji);
					
					
					}
					
					array.put(a, isChecked);
					
				}else{
					
					
					array.delete(a);
					
					if(indext.contains(list.get(a).getId())){
					indext.remove(indext.indexOf(list.get(a).getId()));
					for (int i = 0; i < Main_Bianji_QiYe_InformationActivity.yiji_list.size(); i++) {
						if(Main_Bianji_QiYe_InformationActivity.yiji_list.get(i).getId()==list.get(a).getId()){
							Main_Bianji_QiYe_InformationActivity.yiji_list.remove(i);
						}
					}
					}
				}
				
			}
		});
		
		mHolder.hangye_checkbox.setChecked(array.get(position));
		
		return convertView;
	}
class Holder {
	TextView textview_hangye;
	CheckBox hangye_checkbox;
}

}
